package com.itedge.solutionmanager.service.task.impl.solution;

import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itedge.solutionmanager.domain.impl.Address;
import com.itedge.solutionmanager.domain.impl.ContactPerson;
import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.solutionmanager.domain.impl.Solution;
import com.itedge.solutionmanager.enumeration.CustomerStatus;
import com.itedge.infrastructure.service.domain.IEntityService;
import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.solutionmanager.taskdata.impl.solution.RegisterCustomerTaskData;

@Service
public class RegisterCustomerTaskService implements ITaskService<RegisterCustomerTaskData, Solution> {

    @Autowired
    protected TaskService taskService;
    @Autowired
    private IProcessEntityService<Solution> solutionService;
    @Autowired
    private IProcessService<Customer> customerProcessService;
    @Autowired
    private IEntityService<ContactPerson> contactPersonService;  
    @Autowired
    private ITaskDataEntityService<RegisterCustomerTaskData> registerCustomerTaskDataService;
	
	@Override
	@Transactional
	public void completeTask(String taskId, Solution linkedObject,
			RegisterCustomerTaskData taskData, String user) {
    	registerCustomerTaskDataService.merge(taskData);
    	if (Boolean.FALSE.equals(taskData.getIsExistingCust())) {
    		// create new customer contact person from task data
    		ContactPerson customerContact = new ContactPerson();
    		customerContact.setFirstName(taskData.getContactPersonFirstName());
    	  	customerContact.setSecondName(taskData.getContactPersonSecondName());
    	  	customerContact.setTitle(taskData.getContactPersonTitle());
    	  	Address customerAddress = new Address();
    	  	customerAddress.setTelNumber(taskData.getContactPersonPhone());
    	  	customerContact.setAddress(customerAddress);
    	  	customerContact.setNotes(taskData.getContactPersonNotes());
    	  	// create new customer from task data
    	  	Customer newCustomer = new Customer();
    	  	newCustomer.setStatus(CustomerStatus.ACTIVE);
    	  	newCustomer.setName(taskData.getCustomerName());
    	  	newCustomer.setDescription(taskData.getCustomerDescription());
    	  	contactPersonService.persist(customerContact);
    	  	newCustomer.addContactPerson(customerContact);
    	  	//set to solution
    	  	linkedObject.setCustomer(newCustomer);
    	  	//start new customer process
    	  	customerProcessService.createEntityProcess(newCustomer);
    	} else {
    		linkedObject.setCustomer(taskData.getAssignedCustomer());
    	}
    	solutionService.merge(linkedObject);
    	taskService.setAssignee(taskId, user);
    	taskService.complete(taskId);	
	}

}
